<template>
	<div class="admin-light">
		<div class="task-search">
			<div class="task-search_martio">
				<div class="task-search_text">轻应用</div>
				<div class="task-search_box">
					<i class="el-icon-search"></i>
					<el-input placeholder="请输入搜索内容" v-model="keyword" @input="selectClick"></el-input>
				</div>
			</div>
		</div>

		<div class="admin-light-flex" v-if="viewModel">
			<div class="admin-light-list" v-for="(item, index) in viewModel" :key="index" @click="linkTo(item)" :title="item.name">
				<div class="admin-light-icon">
					<x-image style="width: 100%;height: 100%;" :src="item.image"></x-image>
				</div>
				<div class="admin-light-cont">
					<div class="admin-light-title">{{item.name}}</div>
					<div class="admin-light-intro">
						<span class="admin-light-span" v-for="(itemChild, indexChild) in item.menus" :key="index+indexChild" v-show="indexChild < 2">{{itemChild.name}}、</span>
					</div>
				</div>
			</div>
		</div>
		<zk-nodata v-if="viewModel &&viewModel.length===0"></zk-nodata>
	</div>
</template>

<script>
	import './var.scss'
	import ala from 'ala'
	export default {
		data() {
			return {
				viewModel: null,
				widgetModel: {},
				keyword: '',
				selectModel: {},
				currentModel: null
			}
		},
		props: {
			widget: {}
		},
		mounted() {
			this.init()
		},
		methods: {
			async init() {
				this.$ala.localRemove('admin_mp_adminLinks')
				this.currentModel = await this.$ala.vuexLocalGet(this.$ala.themeCurrentMenuCacheKey())
				this.viewModel = await this.$api.lightAppSearch(this.keyword)
				var this_ = this
				this.viewModel.forEach((res, index) => {
					this_.$set(res, 'menus', res.menu)
					this_.convertName(res)
				})
				this.widgetModel = this.$ala.vuexLocalGet(ala.themeRoleCacheKey(), true)
			},
			linkTo(data) {
				var arrayList = null
				if (data.menus.menus.length !== 0) {
					arrayList = [data.menus]
				}
				this.$ala.localSet('admin_mp_adminLinks', arrayList)
				this.$bus.$emit('admin_mp_menus', arrayList)
				this.$router.push(data.url)
			},
			convertName(vData) {
				var this_ = this
				this_.$set(vData.menus, 'name', vData.menus.value)
				if (vData.menus && vData.menus.menus && vData.menus.menus.length !== 0) {
					vData.menus.menus.forEach((res, index) => {
						this_.$set(res, 'name', res.value)
						this_.$set(res, 'id', index + 1)
						this_.$set(res, 'showChildMenu', true)
						this_.$set(res, 'oneIndex', this_.currentModel.oneIndex)
					})
				}
				return vData
			},
			selectClick() {
				if (this.keyword !== '') {
					var data = []
					this.selectModel.menus.forEach(element => {
						if (element.name.indexOf(this.keyword) > -1) {
							data.push(element)
						}
					})
					this.viewModel.menus = data
				} else {
					this.viewModel = JSON.parse(JSON.stringify(this.selectModel))
				}
			}
		}
	}
</script>